/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package retrieval;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import util.FileListing;

/**
 *
 * @author Danang
 */
public class DocumentModel {
    // List <Token, ArrayList posisi>
    HashMap<String,QueryTermPositionPerField> listQuery;
    
    private int id;
    private int rank;
    private double weight;
    private long docLength;
    
    public DocumentModel(int id) {
        this.id = id;
        listQuery = new HashMap<String, QueryTermPositionPerField>();
        docLength = 0; // not set yet
    }
    
    public void addToken(String token, int field, ArrayList<Integer> a) {
        QueryTermPositionPerField qt;
        if (listQuery.containsKey(token)) {
            qt = listQuery.get(token);
        } else {
            qt = new QueryTermPositionPerField(token);
            listQuery.put(token, qt);
        }
        qt.addQueryListPosition(field, a);
    }

    public int getQueryTF(String query, int field) {
        if (!listQuery.containsKey(query)) {
            return 0;
        }
        return this.listQuery.get(query).getQueryTf(field);
    }
    
    public ArrayList<Integer> getQueryPosition(String query, int field) {
        return this.listQuery.get(query).getQueryPosition(field);
    }
    
    public String toOutputFile(String query) {
        StringBuilder sb = new StringBuilder();
        String separator = " "+FileListing.tabChar;
        sb.append(query).append(separator).append(id).append(separator).append(rank).append(separator).append(weight).append(FileListing.newLine);
        
        return sb.toString();
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Document ID : ").append(getId()).append(FileListing.newLine);
        sb.append("Document Length : ").append(getDocLength()).append(FileListing.newLine);
        sb.append("Rank : ").append(getRank()).append("  Weigth : ").append(getWeight()).append(FileListing.newLine);
        Iterator iter = listQuery.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entries = (Map.Entry) iter.next();
            sb.append(entries.getValue().toString());
        }
        return sb.toString();
    }

    /**
     * @return the weight
     */
    public double getWeight() {
        return weight;
    }

    /**
     * @param weight the weight to set
     */
    public void setWeight(double weight) {
        this.weight = weight;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the rank
     */
    public int getRank() {
        return rank;
    }

    /**
     * @param rank the rank to set
     */
    public void setRank(int rank) {
        this.rank = rank;
    }

    /**
     * @return the docLength
     */
    public long getDocLength() {
        return docLength;
    }

    /**
     * @param docLength the docLength to set
     */
    public void setDocLength(long docLength) {
        this.docLength = docLength;
    }
    
    public void calculateWeight() {
        
    }
    
}
